import { Query } from '../utils/Query'
import { Draw } from '../utils/Draw'
import { delF } from '../utils/DelFeature'
import { DOCMAP, IP, PORT } from '../constants'
var docLayer = new Zondy.Map.Doc('', DOCMAP, {
  IP,
  PORT,
})
export const cancel = (e, map) => {
  if (e == 4) {
    // console.log(map.getLayers().getArray())
    map.un('click', window.handleClick)
    // console.log(map)
    /* 添加交互式画笔,获取图形 */
    let source = new ol.source.Vector({ wrapX: false })
    let layer = new ol.layer.Vector({
      title: '取消收藏',
      source,
    })
    map.addLayer(layer)
    let draw = null
    const DrawActive = () => {
      draw = Draw.createDraw({
        type: 'Box',
        source,
        callback: handleDraw,
      })
      // console.log(draw)
      map.addInteraction(draw)
    }
    DrawActive()
    function handleDraw(e) {
      source.clear()
      // console.log(e)
      const geom = e.feature.getGeometry()
      /* 根据图形查询,获取fid  */
      const service = { docName: DOCMAP, layerId: 5 }
      Query.queryByGeom({
        geom,
        service,
        callback: handleQuery,
      })
    }
    function handleQuery(result) {
      var format = new Zondy.Format.PolygonJSON()
      //将MapGIS要素JSON反序列化为ol.Feature类型数组
      var features = format.read(result)
      // var fid = features[0].id_;
      var fids = features.map((item) => item.id_)
      const service = { docName: DOCMAP, layerId: 5 }
      delF.delFeature({
        fids,
        service,
        docLayer,
      })
    }
    map.on('dblclick', handleDblClick)
    function handleDblClick() {
      source.clear
    }
  }
}
